<?php
/*
 * SQL Generator
 * 用于sql语句构建
 * 
 * @since 
 *      1.0 2014-8-22 by sutroon
 */
require 'Inc/incDbHead.php';

if ($action == 'showconnection') {
    $r = mysql_query("SELECT DATABASE()") or die(mysql_error());
    die('DB:' . mysql_result($r, 0));
}
?>
<!DOCTYPE html>
<html>
    <head>
        <title>SoSQLGenerator</title>
        <meta charset="UTF-8">
        <style type='text/css'>
            body { font-size:12px;}
            table{width:100%; border: solid 1px #CCC; border-left:none; border-bottom: none;}
            th:first-child{width:30px;}
            .th-field {width:500px;}
            th, td { padding: 5px; border:solid 1px #CCC; border-right: none; border-top: none;}
            tr.tr-alt { background-color: #efefef;}
            a { text-decoration: none;}
            fieldset{ margin-bottom:10px; width:960px;}
            fieldset textarea { width:100%; min-height: 90px;}
        </style>
        <script type="text/javascript" src="/Public/Script/jquery-1.11.1.min.js"></script>
    </head>
    <body>
        <?php
        // 显示数据库登录表单
        if (!$conn_info) {
            echoDbLoginForm();
        }
        // 全局菜单
        include 'Inc/incMenu.php';

        $table = isset($_GET['table']) ? $_GET['table'] : ''; // 获取当前选中的数据表名称
        // 显示所有数据表的下拉框
        $result = mysql_query('show tables;'); // 读取所有数据表 
        // 生成数据表下拉控件
        $str = '<div><select id="table-name" onchange="location.href=\'?table=\'+this.value"><option>请选择</option>';
        $droptables = '';
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            $str.='<option value="' . $row[0] . '"' . ($table && $row[0] == $table ? ' selected="selected"' : '') . '>' . $row[0] . '</option>';
            $droptables.='delete from ' . $row[0].';' . PHP_EOL;
        }
        $str.='</select><input type="button" value="Loginoff" onclick="location.href=\'?action=loginoff\'" /> <a href="SoProjectManager.php" target="_blank">[ProjMgr]</a></div>';
        echo $str;

        // 如果有选中表则显示表结构
        if ($table) {
            // 显示表结构语句 2014-8-22 by sutroon
            $str = '<fieldset><legend>表结构</legend><div style="width:100%; height:240px; overflow:scroll">';
            $str.=get_table_schema_text($table);
            $str.='</div></fieldset>';
            echo $str;

            // 显示视图构建语句 2014-8-23 by sutroon
            echo '<fieldset><legend>视图构建语句</legend>';
            echo '<textarea cols="120" rows="6">' . get_create_view_from_table_sql($table) . PHP_EOL . '</textarea>';
            echo '</fieldset>';

            // 显示Select语句 2014-8-23 by sutroon            
            echo '<fieldset><legend>Select语句</legend>';
            echo '<textarea cols="120" rows="6">' . get_select_table_sql($table) . PHP_EOL . '</textarea>';
            echo '</fieldset>';

            // 显示Insert语句 2014-8-23 by sutroon
            $arrout = get_insert_table_sql($table);
            echo '<fieldset><legend>Insert语语句</legend>';
            echo '<textarea cols="120" rows="18">';
            echo $arrout[0] . PHP_EOL;
            echo '=========Lite============' . PHP_EOL;
            echo $arrout[1] . PHP_EOL;
            echo '=========Default=========' . PHP_EOL;
            echo $arrout[2] . PHP_EOL;
            echo '</textarea>';
            echo '</fieldset>';

            // 显示Update语句 2014-8-23 by sutroon            
            echo '<fieldset><legend>Update语句</legend>';
            echo '<textarea cols="120" rows="6">' . get_update_table_sql($table) . PHP_EOL . '</textarea>';
            echo '</fieldset>';

            // 显示Update-Reset语句 2014-9-11 by sutroon            
            echo '<fieldset><legend>Update-Reset语句</legend>';
            echo '<textarea cols="120" rows="6">' . get_update_reset_table_sql($table) . PHP_EOL . '</textarea>';
            echo '</fieldset>';

            // 显示变量名称 2014-8-23 by sutroon            
            echo '<fieldset><legend>变量参数</legend>';
            echo '<textarea cols="120" rows="6">' . get_table_varnames_text($table) . PHP_EOL . '</textarea>';
            echo '</fieldset>';

            // 显示变量注释 2014-8-23 by sutroon    
            echo '<fieldset><legend>变量注释</legend>';
            echo '<textarea cols="120" rows="6">' . get_table_varcomments_text($table) . PHP_EOL . '</textarea>';
            echo '</fieldset>';

            // 显示Insert扩展字段 2014-11-12 by sutroon
            echo '<fieldset><legend>Insert扩展字段</legend>';
            echo '<textarea cols="120" rows="6">' . get_customer_field_insert_text($table) . PHP_EOL . '</textarea>';
            echo '</fieldset>';
        }
        // 显示删除表语句 2014-9-3 by sutroon
        $str = '<fieldset><legend>Delete Tables语句：</legend><textarea>';
        $str.=$droptables;
        $str.='</textarea></fieldset>';
        echo $str;

        // 释放资源
        mysql_free_result($result);
        unset($db);
        ?>
    </body>
    <script type="text/javascript">
        // fieldset标题点击收起或展开 2014-8-22 by sutroon
        $('legend').attr('title', '点击展开或收起').click(function() {
            $(this).next().toggle();
        })
    </script>
</html>


